package org.infodavid.common.persistence;

import java.io.Serializable;
import java.sql.Connection;

import javax.persistence.PersistenceException;
import javax.sql.DataSource;
import javax.sql.XAConnection;
import javax.sql.XADataSource;

/**
 * The Class ConnectionHelper.
 */
public interface IConnectionHelper extends Serializable {
	/** The Constant HOST_EXPRESSION. */
	String HOST_EXPRESSION = "${host}";

	/** The Constant PASSWORD_EXPRESSION. */
	String PASSWORD_EXPRESSION = "${password}";

	/** The Constant SCHEMA_EXPRESSION. */
	String SCHEMA_EXPRESSION = "${schema}";

	/** The Constant USER_EXPRESSION. */
	String USER_EXPRESSION = "${user}";

	/**
	 * Open a new JDBC connection using the given parameters.
	 * @param conf the connection parameters
	 * @return the connection
	 * @throws PersistenceException the persistence exception
	 */
	Connection openConnection(IConnectionConfiguration conf) throws PersistenceException;

	/**
	 * Open data source.
	 * @param conf the connection parameters
	 * @return the data source
	 * @throws PersistenceException the persistence exception
	 */
	DataSource openDataSource(IConnectionConfiguration conf) throws PersistenceException;

	/**
	 * Open XA connection.
	 * @param conf the connection parameters
	 * @return the distributed connection
	 * @throws PersistenceException the persistence exception
	 */
	XAConnection openXaConnection(IConnectionConfiguration conf) throws PersistenceException;

	/**
	 * Open XA data source.
	 * @param conf the connection parameters
	 * @return the distributed data source
	 * @throws PersistenceException the persistence exception
	 */
	XADataSource openXaDataSource(IConnectionConfiguration conf) throws PersistenceException;

	/**
	 * Creates the new schema using the system connection.
	 * @param connection the connection
	 * @param configuration the configuration
	 * @throws PersistenceException the persistence exception
	 */
	void createSchema(Connection connection, IConnectionConfiguration configuration)
	    throws PersistenceException;

	/**
	 * Creates the user.
	 * @param connection the connection
	 * @param username the user name
	 * @param password the password
	 * @throws PersistenceException the persistence exception
	 */
	void createUser(Connection connection, String username, String password)
	    throws PersistenceException;

	/**
	 * Delete user.
	 * @param connection the connection
	 * @param username the user name
	 * @throws PersistenceException the persistence exception
	 */
	void deleteUser(Connection connection, String username) throws PersistenceException;

	/**
	 * Checks if the schema exists using the system connection.
	 * @param connection the connection
	 * @param name the name of the schema
	 * @return true, if successful
	 * @throws PersistenceException the persistence exception
	 */
	boolean hasSchema(Connection connection, String name) throws PersistenceException;

	/**
	 * Build the connection string using the given parameters.
	 * @param conf the connection parameters
	 * @return the connection string
	 * @throws PersistenceException the persistence exception
	 */
	String getConnectionString(IConnectionConfiguration conf) throws PersistenceException;

	/**
	 * Gets the type.
	 * @return the type
	 */
	String[] getTypes();
}
